import 'package:flutter/material.dart';
import 'package:get/get_state_manager/src/rx_flutter/rx_obx_widget.dart';
import 'package:intl/intl.dart';
import 'package:seedfour/app/constants.dart';
import 'package:seedfour/app/models/case_detail_entity.dart';
import 'package:seedfour/app/theme.dart';
import 'package:seedfour/app/views/module_case/case_detail/state.dart';

class ApproveListWidget extends StatelessWidget {
  const ApproveListWidget({
    Key? key,
    required this.data,
  }) : super(key: key);

  final CaseDetailState data;

  @override
  Widget build(BuildContext context) {
    return Container(
      margin: EdgeInsets.symmetric(horizontal: kSpaceSmall),
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(kRadiusMedium),
        color: Colors.white,
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Padding(
            padding: EdgeInsets.all(kSpaceMedium),
            child: Text(
              '审批处理过程',
              style: TextStyle(
                fontSize: kFontSizeMedium,
                color: AppColor.colorFontBlack,
                fontWeight: AppFont.fontMedium,
              ),
            ),
          ),
          const Divider(),
          _buildApproveItemBg(builder: (item) {
            return [_buildApproveItem(item: item)];
          }),
        ],
      ),
    );
  }

  Widget _renderRow(String title, String content) {
    return Padding(
      padding:
          EdgeInsets.symmetric(horizontal: kSpaceMedium, vertical: kSpaceMini),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Text(
            title,
            style: TextStyle(
              fontSize: kFontSizeSmall,
              color: AppColor.colorFontBlack,
            ),
          ),
          Text(
            content,
            style: TextStyle(
              fontSize: kFontSizeSmall,
              color: AppColor.colorFontBlack,
            ),
          ),
        ],
      ),
    );
  }

  Widget _buildApproveItem({required CaseApproveRecord item}) {
    return Column(
      children: [
        SizedBox(height: kSpaceMini),
        _renderRow('审批类型：', approveType[item.type!] ?? ''),
        _renderRow(
            '审批结果：',
            item.type != 4
                ? approveStatus[item.approve!] ?? ''
                : sealStatus[item.approve!] ?? ''),
        _renderRow('审批人：', item.approverName ?? ''),
        _renderRow(
          '申请时间：',
          item.createdAt == null
              ? ''
              : DateFormat('yyyy-MM-dd HH:mm')
                  .format(DateTime.fromMillisecondsSinceEpoch(item.createdAt!)),
        ),
        _renderRow(
          '审批时间：',
          item.updatedAt == null
              ? ''
              : DateFormat('yyyy-MM-dd HH:mm')
                  .format(DateTime.fromMillisecondsSinceEpoch(item.updatedAt!)),
        ),
        _renderRow('备注：', item.remark ?? ''),
      ],
    );
  }

  Widget _buildApproveItemBg(
      {required List<Widget> Function(CaseApproveRecord item) builder}) {
    return Obx(() {
      return Column(
        children: [
          ...builder(data.caseApproveList[0]),
          SizedBox(height: kSpaceSmall),
        ],
      );
    });
  }
}
